package com.zp.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.entity.ClinicVsPrice;
import com.zp.base.entity.bo.ClinicVsPriceBo;
import com.zp.base.entity.vo.ClinicVsPriceVo;
import com.zp.base.mapper.ClinicVsPriceMapper;
import com.zp.base.service.IClinicVsPriceService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 诊疗项目与价对照Service业务层处理
 *
 * @author zhang peng
 * @date 2023-01-09
 */
@RequiredArgsConstructor
@Service
public class ClinicVsPriceServiceImpl implements IClinicVsPriceService {

    private final ClinicVsPriceMapper baseMapper;

    /**
     * 查询诊疗项目与价对照
     */
    @Override
    public ClinicVsPriceVo queryById (String clinicItemId) {
        return baseMapper.selectVoById(clinicItemId);
    }

    /**
     * 查询诊疗项目与价对照列表(分页)
     */
    @Override
    public TableDataInfo<ClinicVsPriceVo> queryPageList (ClinicVsPriceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ClinicVsPrice> lqw = buildQueryWrapper(bo);
        Page<ClinicVsPriceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询诊疗项目与价对照列表(不分页)
     */
    @Override
    public List<ClinicVsPriceVo> queryList (ClinicVsPriceBo bo) {
        LambdaQueryWrapper<ClinicVsPrice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ClinicVsPrice> buildQueryWrapper (ClinicVsPriceBo bo) {
        LambdaQueryWrapper<ClinicVsPrice> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getClinicItemId()), ClinicVsPrice::getClinicItemId, bo.getClinicItemId());
        lqw.eq(StringUtils.isNotBlank(bo.getPriceId()), ClinicVsPrice::getPriceId, bo.getPriceId());
        lqw.eq(bo.getAmount() != null, ClinicVsPrice::getAmount, bo.getAmount());
        return lqw;
    }

    /**
     * 新增诊疗项目与价对照
     */
    @Override
    public Boolean insertByBo (ClinicVsPriceBo bo) {
        ClinicVsPrice add = BeanUtil.toBean(bo, ClinicVsPrice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setClinicItemId(add.getClinicItemId());
        }
        return flag;
    }

    /**
     * 修改诊疗项目与价对照
     */
    @Override
    public Boolean updateByBo (ClinicVsPriceBo bo) {
        ClinicVsPrice update = BeanUtil.toBean(bo, ClinicVsPrice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (ClinicVsPrice entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除诊疗项目与价对照
     */
    @Override
    public Boolean deleteClinicItem (ClinicVsPriceBo bo) {
        return baseMapper.delete(Wrappers.<ClinicVsPrice>lambdaQuery()
                .eq(ClinicVsPrice::getPriceId, bo.getPriceId())
                .eq(ClinicVsPrice::getClinicItemId, bo.getClinicItemId())) > 0;
    }
}
